<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="标题">
      <van-icon name="cross" class="nav-icon" slot="left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 导航栏 -->
    <!-- 登录表单 -->
  <van-form @submit="onSubmit" ref="loginForm">
    <!-- 用户名输入框 -->
  <van-field
    name="mobile"
    placeholder="请输入手机号"
    v-model="user.mobile"
    type="number"
     maxlength="11"
    :rules="userFormRules.mobile">
    <i slot="left-icon" class="toutiao toutiao-shouji"></i>
  </van-field>
    <!-- 验证码输入框 -->
  <van-field
    name="code"
    v-model="user.code"
    :rules="userFormRules.code"
    type="number"
    maxlength="6"
    placeholder="请输入验证码">
  <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
   <template #button>
    <van-count-down :time="time" format="ss 秒" v-if="isCountshow"
    @finish="isCountshow=false"/>
    <van-button size="small"
    v-else
    native-type="button" class="send-sms-btn" round type="default" @click="onSendSms" >发送验证码</van-button>
    <!-- default 背景色 -->
  </template>
  </van-field>
  <!-- 登录按钮 -->
  <div style="margin: .2rem;" class="login-btn-wrap">
    <van-button block type="info" native-type="submit" class="login-btn" >登录</van-button>
  </div>
</van-form>
    <!-- 登录表单 -->
  </div>
</template>

<script>
import { login, setSms } from '@/api/user.js'
export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        mobile: '', // 手机号
        code: '246810' // 验证码
      },
      userFormRules: {
        mobile: [{ required: true, message: '请填写手机号' },
          { patter: /^1[3/5/7/8]\d{9}$/, message: '手机格式错误' }],

        code: [{ required: true, message: '请填写验证码' },
          { patter: /^\d{6}$/, message: '验证码格式错误' }]
      },
      time: 1000 * 60,
      isCountshow: false
    }
  },
  methods: {
    // 登录
    async onSubmit () {
      this.$toast.loading({
        message: '登陆中',
        forbidClick: true,
        duration: 0
      })
      const user = this.user
      try {
        const { data } = await login(user)
        this.$toast.success('登陆成功')
        this.$store.commit('setUser', data.data)
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码输入错误')
          console.log(err.response)
          console.log('手机号或验证码输入错误', err)
        } else {
          this.$toast.fail('登陆失败')
          console.log('登陆失败', err)
        }
      }
    },
    // 点击发送验证码按钮
    async onSendSms () {
      try {
        await this.$refs.loginForm.validate('mobile')
        // console.log('验证成功')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 验证成功之后操作
      this.isCountshow = true
      // 掉验证码发送接口
      try {
        await setSms(this.user.mobile)
        console.log(this.user.mobile)
        console.log('发送验证码成功')
      } catch (err) {
        this.isCountshow = false
        if (err.response.status === 429) {
          console.log('发送频繁,请稍后')
        } else {
          console.log('发送失败')
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .nav-icon{
    color:#fff
  }
  .toutiao {
    font-size: .4625rem;
  }
  .send-sms-btn {
    width: 180px;
    height: .575rem;
    line-height: .575rem;
    background-color: #ededed;
    font-size: .275rem ;
    color: #666;
  }
  .login-btn-wrap {
    padding: .6625rem .4125rem;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
